<?php

 function MailEvent($event,$mid,$joinid,$to,$tid)
 {
     $sql="select * from partners_admin";
     $ret1=mysql_query($sql);
     $row=mysql_fetch_object($ret1);  //common header and footer
     $adminheader=stripslashes($row->admin_mailheader);
     $adminfooter=stripslashes($row->admin_mailfooter);
	 $admin_email = stripslashes($row->admin_email);

	//modified on 12-July-2006 to get the merchnat email address
	if($mid)
	{
		$sql_mer  =  "SELECT * FROM partners_login WHERE login_id='$mid' AND login_flag='m'";
		$res_mer  =  mysql_query($sql_mer);
		if(mysql_num_rows($res_mer) > 0)
		{
			$row_mer = mysql_fetch_object($res_mer);
			$mer_mail = $row_mer->login_email;
			$mer_password     =stripslashes($row_mer->login_password);
		}
		
		$sql_mer1 = "SELECT * FROM partners_merchant WHERE merchant_id='$mid'";
		$res_mer1 = mysql_query($sql_mer1);
		if(mysql_num_rows($res_mer1) > 0)
		{
			$row_mer1 = mysql_fetch_object($res_mer1);
			$mer_firstname        =stripslashes($row_mer1->merchant_firstname);
			$mer_lastname         =stripslashes($row_mer1->merchant_lastname);
			$mer_company          =stripslashes($row_mer1->merchant_company);
			$mer_loginlink        =stripslashes($row_mer1->merchant_url);
		}
	}
	
		   $sql1="select * from partners_joinpgm where joinpgm_id=$joinid";
           $result1=mysql_query($sql1);

           if(mysql_num_rows($result1)>0)
           {
			   $rows=mysql_fetch_object($result1);
			   $aid=$rows->joinpgm_affiliateid;
			   $pid=$rows->joinpgm_programid;
           }
          
		   $sql1="select * from partners_affiliate where affiliate_id=$aid";
           $result1=mysql_query($sql1);
          // echo $sql1;
           if(mysql_num_rows($result1)>0)
		   {
                $rows             =mysql_fetch_object($result1);
                $firstname        =stripslashes($rows->affiliate_firstname);
                $lastname         =stripslashes($rows->affiliate_lastname);
                $company          =stripslashes($rows->affiliate_company);
                $loginlink        =stripslashes($rows->affiliate_url);
		   }
           
		   $sql1="select * from partners_login where login_id=$aid and login_flag='a'";
           $result1=mysql_query($sql1);
           if(mysql_num_rows($result1)>0)
		   {
                $rows         =mysql_fetch_object($result1);
                $password     =stripslashes($rows->login_password);
                $affemail     =stripslashes($rows->login_email);
		   }
		   
           $sql1="select * from partners_program where program_id=$pid";
           $result1=mysql_query($sql1);
           if(mysql_num_rows($result1)>0)
		   {
                $rows        =mysql_fetch_object($result1);
                $program     =stripslashes($rows->program_url);
		   }
		   
          $sql1="select * from partners_transaction where transaction_id=$tid";
          $result1=mysql_query($sql1);
          if(mysql_num_rows($result1)>0)
		  {
                $rows         =mysql_fetch_object($result1);
                $type         =stripslashes($rows->transaction_type);
                $date         =stripslashes($rows->transaction_dateoftransaction);
                $commission   =stripslashes($rows->transaction_amttobepaid);
		   }
		   $today         =date("Y-m-d");
	
	//End modify on 12-July-2006 

     $sql="select * from partners_mermail where mermail_eventname='$event' and mermail_merchantid=$mid";
     $result=mysql_query($sql);
     //echo $sql;
     if(mysql_num_rows($result)>0)
      {
           $row=mysql_fetch_object($result);
           //echo $firstname;
           $sub           =stripslashes($row->mermail_subject);
           $message       =stripslashes($row->mermail_message);
           $head          =stripslashes($row->mermail_header);
           $footer        =stripslashes($row->mermail_footer);
           $from          =stripslashes($row->mermail_from);
           

         //echo $message;
                $toaddress  = stripslashes($to);
                $subject        = stripslashes($sub);

                    $headers        =  "Content-Type: text/html; charset=iso-8859-1\n";
                    $headers       .=  "From: $from\n";
					$headers		=str_replace("[from]",$mer_mail,$headers);
                    $body="<table border ='0' cellpadding='0' cellspacing='0' width='90%' >";
                    $body.="<tr>";
                    $body.="<td width='100%' align='center' valign='top'><br/>";
                    $body.="<table border='0' cellpadding='0' cellspacing='0'  width='80%'>";

                    $body.="<tr>";
                    $body.="<td  width='100%' align='center'> $adminheader</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'> $head</td>";
                    $body.="</tr>";

                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>&nbsp;</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td width='100%' align='left'>$message";
                    $body.="</td></tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>&nbsp;</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>$footer</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='center'>$adminfooter</td>";
                    $body.="</tr>";

                    $body.="</table>";
                    $body.="</td>";
                    $body.="</tr>";
                    $body.="</table>";


                     $body=str_replace("[firstname]",$firstname,$body);
                     $body=str_replace("[lastname]",$lastname,$body);
                     $body=str_replace("[company]",$company,$body);
                     $body=str_replace("[affemail]",$affemail,$body);
                     $body=str_replace("[loginlink]",$loginlink,$body);
                     $body=str_replace("[from]",$mer_mail,$body);
                     $body=str_replace("[password]",$password,$body);
                     $body=str_replace("[commission]",$commission,$body);
                     $body=str_replace("[program]",$program,$body);
                     $body=str_replace("[type]",$type,$body);
                     $body=str_replace("[date]",$date,$body);
                     $body=str_replace("[today]",$today,$body);



      }
     else
       {
           $sql="select * from partners_adminmail where adminmail_eventname='$event' ";
           $result             =mysql_query($sql);
           $row                =mysql_fetch_object($result);
           $sub                =stripslashes($row->adminmail_subject);
           $message            =stripslashes($row->adminmail_message);
           $head               =stripslashes($row->adminmail_header);
           $footer             =stripslashes($row->adminmail_footer);
           $from               =stripslashes($row->adminmail_from);
           $toaddress          =stripslashes($to);
           $subject            =stripslashes($sub);

                    $headers        =  "Content-Type: text/html; charset=iso-8859-1\n";
                    $headers       .=  "From: $from\n";
					$headers		=str_replace("[from]",$admin_email,$headers); 
                    $body="<table border ='0' cellpadding='0' cellspacing='0' width='90%' >";
                    $body.="<tr>";
                    $body.="<td width='100%' align='center' valign='top'><br/>";
                    $body.="<table border='0' cellpadding='0' cellspacing='0'  width='80%'>";
                    $body.="<td  width='100%' align='left'> $head</td>";
                    $body.="</tr>";

                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>&nbsp;</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td width='100%' align='left'>$message";
                    $body.="</td></tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>&nbsp;</td>";
                    $body.="</tr>";
                    $body.="<tr>";
                    $body.="<td  width='100%' align='left'>$footer</td>";
                    $body.="</tr>";

                    $body.="</table>";
                    $body.="</td>";
                    $body.="</tr>";
                    $body.="</table>";
					
					//Replace variable in the content with values
                     $body=str_replace("[aff_firstname]",$firstname,$body);
                     $body=str_replace("[aff_lastname]",$lastname,$body);
                     $body=str_replace("[aff_company]",$company,$body);
                     $body=str_replace("[aff_email]",$affemail,$body);
                     $body=str_replace("[aff_loginlink]",$loginlink,$body);
                     $body=str_replace("[aff_password]",$password,$body);
                     
                     $body=str_replace("[mer_firstname]",$mer_firstname,$body);
                     $body=str_replace("[mer_lastname]",$mer_lastname,$body);
                     $body=str_replace("[mer_company]",$mer_company,$body);
                     $body=str_replace("[mer_email]",$mer_mail,$body);
                     $body=str_replace("[mer_loginlink]",$mer_loginlink,$body);
                     $body=str_replace("[mer_password]",$mer_password,$body);
					 
					 $body=str_replace("[from]",$admin_email,$body);
                     $body=str_replace("[commission]",$commission,$body);
                     $body=str_replace("[program]",$program,$body);
                     $body=str_replace("[type]",$type,$body);
                     $body=str_replace("[date]",$date,$body);
                     $body=str_replace("[today]",$today,$body);
           }



                mail($toaddress,$subject,$body,$headers);

    }
?>